package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Food;
import com.neusoft.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/FoodController")
public class FoodController {
    @Autowired
    private FoodService foodService;

    @GetMapping("/listFoodByBusinessId/{businessId}")
    public CommonResult listFoodByBusinessId(@PathVariable("businessId") Integer businessId) throws Exception {
        List<Food> list = foodService.listFoodByBusinessId(businessId);
        return new CommonResult(200, "success", list);
    }

    // 修改为 RESTful 风格路径
    @PostMapping("/save/{businessId}/{foodName}/{foodExplain}/{foodPrice}")
    public CommonResult saveFood(
            @PathVariable("businessId") Integer businessId,
            @PathVariable("foodName") String foodName,
            @PathVariable("foodExplain") String foodExplain,
            @PathVariable("foodPrice") Double foodPrice) {
        try {
            int result = foodService.saveFood(businessId, foodName, foodExplain, foodPrice);
            return new CommonResult<>(200, "保存食物信息成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(500, "保存食物信息失败", null);
        }
    }

    @PostMapping("/update")
    public CommonResult updateFood(@ModelAttribute Food updateDto) {
        try {
            int result = foodService.updateFood(
                    updateDto.getFoodId(),
                    updateDto.getBusinessId(),
                    updateDto.getFoodName(),
                    updateDto.getFoodExplain(),
                    updateDto.getFoodPrice(),
                    updateDto.getFoodImg()
            );
            return new CommonResult<>(200, "更新食物信息成功", result);
        } catch (Exception e) {
            return new CommonResult<>(500, "更新食物信息失败", null);
        }
    }


    // 修改为 RESTful 风格路径
    @DeleteMapping("/remove/{businessId}/{foodId}")
    public CommonResult removeFood(
            @PathVariable Integer businessId,
            @PathVariable Integer foodId) {
        try {
            int result = foodService.removeFood(businessId, foodId);
            return new CommonResult<>(200, "删除食物信息成功", result);
        } catch (Exception e) {
            return new CommonResult<>(500, "删除食物信息失败", null);
        }
    }

    @GetMapping("/getFoodById/{foodId}/{businessId}")
    public CommonResult getFoodById(@PathVariable("foodId") Integer foodId,@PathVariable("businessId") Integer businessId) {
        Food food = foodService.getFoodById(foodId, businessId);
        if (food != null) {
            return new CommonResult<>(200, "success", food);
        } else {
            return new CommonResult<>(404, "Food not found", null);
        }
    }
    @GetMapping("/getById/{foodId}")
    public CommonResult getById(@PathVariable("foodId") Integer foodId) {
        Food food = foodService.getById(foodId);
        if (food != null) {
            return new CommonResult<>(200, "success", food);
        } else {
            return new CommonResult<>(404, "Food not found", null);
        }
    }

}
